package year2022.month123;

public class SynchronizedTest {
    public static void main(String[] args) throws InterruptedException {
        Obj obj = new Obj();

        Thread[] tArr = new Thread[10];
        for (int i=0; i<tArr.length; i++){
            tArr[i] = new Thread(new MyInit(obj));
            tArr[i].start();
        }
    }
}

class MyInit implements Runnable{
    private Obj obj;

    public MyInit(Obj obj){
        this.obj = obj;
    }

    @Override
    public void run() {
        for (int i=0; i<1000; i++){
            obj.add();
        }
        System.out.print(obj.get() + "\t");
    }
}

class Obj {
    private int i = 0;

    public int get() {
        return i;
    }

//    public void add() {
    public synchronized void add() {
        this.i++;
    }
}
